package com.pzl.classloader;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * Created by zl.peng on 2016-03-29.
 * No need package,delete it
 * 加密解密class文件
 */
public class ClzEncryption {
    static int mode = 0;

    public static void main(String[] args) {
        String packagePath = "C:";
        Scanner reader = new Scanner(System.in);
        System.out.println("please input mode(1 for Encrypt,2 for Decrypt):");
        mode = reader.nextInt();
        System.out.println("please input classes path (such as H:\\AS_projects\\project_demo\\app\\build\\intermediates\\classes\\debug):");
        packagePath = reader.next();
        packagePath = packagePath.replace("\\", "\\\\");
        File f = new File(packagePath);
        System.out.println("Classes Start --------------------");
        try {
            list(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Classes End --------------------");
    }

    /**
     * 开始加密
     *
     * @throws IOException
     */
    private static void startEncrypt(String absolutePath) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(absolutePath));
        byte[] data = new byte[bis.available()];
        bis.read(data);
        bis.close();
        for (int i = 0; i < data.length; i++) {
            data[i] = (byte) (data[i] + 1);//加密算法
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(absolutePath));
        bos.write(data);
        bos.close();
    }

    /**
     * 开始解密
     *
     * @param absolutePath
     */
    private static void startDecrypt(String absolutePath) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(absolutePath));
        byte[] data = new byte[bis.available()];
        bis.read(data);
        bis.close();
        for (int i = 0; i < data.length; i++) {
            data[i] = (byte) (data[i] - 1);//解密算法
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(absolutePath));
        bos.write(data);
        bos.close();
    }


    /**
     * 遍历class文件,并加密
     *
     * @param file
     * @return
     */
    public static void list(File file) throws IOException {
        File[] fs = file.listFiles();
        if (fs != null) {
            for (File f : fs) {
                if (f.isFile()) {// 如果是文件直接输出
                    String type = f.getName().substring(f.getName().lastIndexOf(".") + 1, f.getName().length());
                    if ("class".equals(type)) {
                        switch (mode) {
                            case 1:
                                startEncrypt(f.getAbsolutePath());
                                System.out.println(f.getName() + "   加密完成");
                                break;
                            case 2:
                                startDecrypt(f.getAbsolutePath());
                                System.out.println(f.getName() + "   解密完成");
                                break;
                        }
                    }
                } else {//目录
                    list(f);
                }
            }
        }
    }
}
